package com.wsq.nutz.dao;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.ConnCallback;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.NutIoc;
import org.nutz.ioc.loader.json.JsonLoader;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.alibaba.druid.pool.DruidDataSource;

public class SimpleDaoTest {

	Ioc ioc;
	Dao dao;
	DruidDataSource ds;
	private static Log log = Logs.get();
	
	//插入单条
	@Test
	public void testInsert() {
		Pet pet = new Pet("jack", "大连", 10);
		dao.insert(pet);
		
		System.out.println(pet.getId());
	}
	//取单条
	@Test
	public void test_fetch(){
		Pet pet = dao.fetch(Pet.class);
		assertNull(pet);
		
		dao.insert(new Pet("wsq", "eee", 10));
		pet = dao.fetch(Pet.class);
        log.debug(pet);
	}
	//取多条
	@Test
	public void test_query(){
		List<Pet> pets = batchAdd();
		dao.fastInsert(pets);
		pets = dao.query(Pet.class, Cnd.where("age",">","18"));
		System.out.println(pets.size());
		//分页
		Pager pager = dao.createPager(2, 10);
		Cnd cnd = Cnd.where("age",">","30");
		pets = dao.query(Pet.class, cnd,pager);
		int count = dao.count(Pet.class, cnd);
		pager.setRecordCount(count);
		log.debug(pager);
		//assertTrue(pets.size()<=10);
	}
	public List<Pet> batchAdd(){
		List<Pet> pets = new ArrayList<Pet>(100);
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			pets.add(new Pet("wsq"+i, "123"+i, r.nextInt(130)));
		}
		return pets;
	}
	
	//批量插入
	@Test
	public void testBatchBInsert() {
		List<Pet> pets = new ArrayList<Pet>(100);
		Random r = new Random();
		for (int i = 0; i < 100; i++) {
			pets.add(new Pet("wsq"+i, "123"+i, r.nextInt(130)));
		}
		assertEquals(100, pets.size());
		dao.fastInsert(pets);
	}
	
	//更新
	@Test
	public void test_update(){
		dao.fastInsert(batchAdd());
		Pet pet = dao.fetch(Pet.class , 69);
		int oldAge = pet.getAge();
		pet.setAge(oldAge+30);
		dao.update(pet);
		assertEquals(oldAge+30, dao.fetch(Pet.class,69).getAge());
		//批量更新
		//dao.update(Pet.class, Chain.make("age", "90"), Cnd.where("age", ">", 30));
	}
	
	
	//删除
	@Test
	public void test_delete(){
		dao.fastInsert(batchAdd());
		Pet pet = dao.fetch(Pet.class , 69);
		//删除单个
		dao.delete(pet);
		assertNull(dao.fetch(Pet.class , 69));
        //批量删除
		dao.clear(Pet.class, Cnd.where("age",">",30));
		System.out.println(dao.count(Pet.class, Cnd.where("age",">",30)));
	}
	
	//自定义SQL
	@Test
	public void test_customSql(){
		dao.fastInsert(batchAdd());
        Sql sql = Sqls.fetchLong("select id from t_pet where nm=@name");
        sql.setParam("name", "wsq11");
        dao.execute(sql);
        long id = sql.getObject(Long.class);
        System.out.println(id);
        assertEquals(12, id);
	}
	
	//使用JDBC原始方法进行操作
	@Test
	public void test_collection(){
		dao.run(new ConnCallback() {
			@Override
			public void invoke(Connection conn) throws Exception {
				System.out.println(conn.getAutoCommit());
			}
		});
	}
	
	public void init(){
		//每次都重新创建表
		dao.create(Pet.class, true);
	}
	@Before
	public void before(){
		ioc = new NutIoc(new JsonLoader("ioc/dao.js"));
		dao = ioc.get(Dao.class);
		init();
	}
	
	public void before2(){
		ds = new DruidDataSource();
		ds.setDriverClassName("com.alibaba.druid.pool.DruidDataSource");
		ds.setUrl("jdbc:oracle:thin:@localhost:1521:wsq");
		
		dao = new NutDao(ds);
	}
	
	@After
	public void after(){
		if (ioc!=null) {
			ioc.depose();
		}
	}
	
	public void after2(){
		if (ds!=null) {
			ds.close();
		}
	}
}
